package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = false,propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
//可以标记在方法 和 类上,标记在类上,可以再方法也上单独配置
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDao accountDao;


    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public void findAll(){

    }

    @Override
    public void transfer(String fromName, String toName, double money) {
        //1. 根据账户名查询账户信息
        Account fromAccount = accountDao.findByName(fromName);
        Account toAccount = accountDao.findByName(toName);

        //2. 更改账户的余额
        fromAccount.setMoney(fromAccount.getMoney() - money);
        toAccount.setMoney(toAccount.getMoney() + money);

        //3. 更新到数据库
        accountDao.update(fromAccount);
        System.out.println(1/0);
        accountDao.update(toAccount);
    }
}
